package com.bpmn.flow.controller;

import com.bpmn.common.annotation.ResponseInfoWrapper;
import com.bpmn.common.pojo.PageResult;
import com.bpmn.flow.controller.query.ProcessTaskQuery;
import com.bpmn.flow.controller.vo.TaskVo;
import com.bpmn.flow.dto.TaskOperationDTO;
import com.bpmn.flow.service.TaskCarbonCopyService;
import com.bpmn.flow.service.TaskOperationService;
import com.bpmn.flow.service.TaskQueryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author rain
 */
@RestController
@ResponseInfoWrapper
@RequiredArgsConstructor
@RequestMapping("/workflow/task")
@Tag(name = "工作流任务操作")
public class TaskController {

    private final TaskOperationService taskOperationService;

    private final TaskQueryService taskQueryService;

    private final TaskCarbonCopyService taskCarbonCopyService;

    @Operation(summary = "处理任务")
    @PostMapping("/operate")
    public Boolean handleTaskOperation(@Validated @RequestBody TaskOperationDTO dto) {
        taskOperationService.handleTaskOperation(dto);
        return true;
    }

    @Operation(summary = "撤销任务(只有历史任务有此操作)")
    @PostMapping("/revoke")
    public Boolean revokeTask(@RequestBody TaskOperationDTO dto) {
        taskOperationService.handleRevoke(dto);
        return true;
    }

    @Operation(summary = "查询待办")
    @PostMapping("/todo/list")
    public PageResult<TaskVo> queryTaskTodoPage(@RequestBody ProcessTaskQuery pageReqVO) {
        return taskQueryService.queryTaskTodoPage(pageReqVO);
    }

    @Operation(summary = "查询已办")
    @PostMapping("/done/list")
    public PageResult<TaskVo> queryTaskDonePage(@RequestBody ProcessTaskQuery pageReqVO) {
        return taskQueryService.queryTaskDonePage(pageReqVO);
    }

    @Operation(summary = "通过流程实例id查询任务审批记录")
    @PostMapping("/listByProcessInstanceId")
    @Parameter(name = "id", description = "流程实例的编号", required = true)
    public List<TaskVo> getTaskListByProcessInstanceId(@RequestParam(value = "id") String id) {
        return taskQueryService.getTaskListByProcessInstanceId(id);
    }

    @Operation(summary = "执行任务抄送")
    @PostMapping("/carbon-copy/execute")
    @Parameter(name = "taskId", description = "任务ID", required = true)
    @Parameter(name = "userIds", description = "用户ID列表", required = true)
    public Boolean executeTaskCarbonCopy(@RequestParam(value = "taskId") String taskId,
                                         @RequestParam(value = "userIds") List<String> userIds) {
        taskCarbonCopyService.executeTaskCarbonCopy(taskId, userIds);
        return true;
    }

    @Operation(summary = "查询用户的运行时抄送任务列表")
    @PostMapping("/carbon-copy/runtime/list")
    @Parameter(name = "userId", description = "用户ID", required = true)
    public List<TaskVo> getCarbonCopyTasks(@RequestParam(value = "userId") String userId) {
        return taskCarbonCopyService.getCarbonCopyTasks(userId);
    }

    @Operation(summary = "查询用户的历史抄送任务列表")
    @PostMapping("/carbon-copy/history/list")
    @Parameter(name = "userId", description = "用户ID", required = true)
    public List<TaskVo> getHistoricCarbonCopyTasks(@RequestParam(value = "userId") String userId) {
        return taskCarbonCopyService.getHistoricCarbonCopyTasks(userId);
    }

}